<?php
class language extends lib_base_base {
	
	protected $lan;
	
	protected $url_dir;
		
	public $language=array();
	
	public function __construct(){
		
		static $lan;
	    if(!isset($lan)){
	    	if(!isset(R::getClass('requests')->lan) && empty(R::getClass('requests')->lan)){
	    		 $lan=getConfig('language');
	    	}else{
	    		$lan=R::getClass('requests')->lan;
	    	}
	        $this->lan=$lan;
	    }
		
	    $request=R::getClass('requests');
	    $php_self=$request->server['PHP_SELF'];
	    $url_name=basename($php_self,'.php');	
	    $this->url_dir=$url_name;	
	    $path=HZHIHU_ROOT.LANGUAGE_FILE.$this->lan.'/public/';
		if (is_dir($path) && $handle=opendir($path)) {
			while (false !== ($file = readdir($handle))) {
				if ($file!='.' && $file!='..') {
					$Ary=explode('.', $file);
					$fname=array_pop($Ary); 
					if(file_exists( $path.$file) && $fname=='php'){
						require_once($path.$file);
						if(!empty($this->language)){
								$this->language['public']=array_merge($this->language,$language);		
								unset($language);						
						}else{
								$this->language['public']=$language;
						}						
					}					
				}
			}
		}else{
			//$this->ThewError('language is not find');
		}
	}
	
	public function onloadLan($control=''){
		
		$config=R::getClass('config');
		$get=R::exurl();
		$do=$get[0];
		$cn=$get[1];	
		$file=empty($control)?
			HZHIHU_ROOT.LANGUAGE_FILE.$this->lan.'/'.$do.'.php':
				HZHIHU_ROOT.LANGUAGE_FILE.$this->lan.'/'.$control.'.php';
		
		if(file_exists($file)){
			require_once($file);
			if(!empty($this->language)){
				$this->language=array_merge($this->language,$language);
			}else{
				$this->language=$language;
			}
		}else{
			//$this->ThewError('language is not find');
		}
		unset($language);
		return $this->language;
	}
	
	public function __destruct(){
		unset($this->language);
	}
}